﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using ATSV2.Web;
using ATSV2.Web.Services;
using System.ServiceModel.DomainServices.Client;

namespace ATSV2.Views
{
    public partial class AdminCockpit : ChildWindow
    {
        ATSVDataContext _context = new ATSVDataContext();

        public AdminCockpit()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void newsItemSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void speichernButton_Click(object sender, RoutedEventArgs e)
        {
            NewsItemSet newNews = new NewsItemSet();
            newNews.Titel = titelTextBox.Text;
            newNews.Body = bodyTextBox.Text;
            newNews.Link = linkTextBox.Text;
            newNews.CreateDate = DateTime.Now;

            _context.NewsItemSets.Add(newNews);


            _context.SubmitChanges(OnSubmitCompleted, null);

            speichernButton.IsEnabled = _context.HasChanges;
            verwerfenButton.IsEnabled = _context.HasChanges;
        }

        private void OnSubmitCompleted(SubmitOperation so)
        {
            if (so.HasError)
            {
                MessageBox.Show(string.Format("Speichern fehlgeschlagen: {0}", so.Error.Message));
                so.MarkErrorAsHandled();
            }
        }


        private void verwerfenButton_Click(object sender, RoutedEventArgs e)
        {
            _context.RejectChanges();

            titelTextBox.Text = "";
            bodyTextBox.Text = "";
            linkTextBox.Text = "";
        }

        private void spielerSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void teamSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void trainerSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void fotoSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }



        private void triainerSpeichernButton_Click(object sender, RoutedEventArgs e)
        {
            TrainerSet myTrainer = new TrainerSet();

            myTrainer.Name = nameTextBox.Text;
            myTrainer.Vorname = vornameTextBox.Text;
            myTrainer.TeamSets.Add(nameComboBox.SelectedItem as TeamSet);

            if (fotoURLTextBox.Text.Length > 0)
            {
                FotoSet myFoto = new FotoSet();
                myFoto.Name = nameTextBox.Text + ", " + vornameTextBox.Text;

                myFoto.URL = fotoURLTextBox.Text;

                _context.FotoSets.Add(myFoto);
                myTrainer.FotoSets.Add(myFoto);
            }
            _context.TrainerSets.Add(myTrainer);
            _context.SubmitChanges(OnSubmitCompleted, null);

            triainerSpeichernButton.IsEnabled = _context.HasChanges;
            trainerVerwerfenButton.IsEnabled = _context.HasChanges;
        }

        private void trainerVerwerfenButton_Click(object sender, RoutedEventArgs e)
        {
            _context.RejectChanges();

            nameTextBox.Text = "";
            vornameTextBox.Text = "";
            nameComboBox.SelectedIndex = -1;
        }

        private void FotoSpeichernButton_Click(object sender, RoutedEventArgs e)
        {
            FotoSet myFoto = new FotoSet();
            myFoto.Name = nameTextBox.Text + ", " + vornameTextBox.Text;
            myFoto.URL = fotoURLTextBox.Text;

            _context.FotoSets.Add(myFoto);
            _context.SubmitChanges(OnSubmitCompleted, null);

            FotoSpeichernButton.IsEnabled = _context.HasChanges;
            FotoVerwerfenButton.IsEnabled = _context.HasChanges;
        }

        private void FotoVerwerfenButton_Click(object sender, RoutedEventArgs e)
        {
            _context.RejectChanges();

            FototitelTextBox.Text = "";
            FotoURLTextBox.Text = "";            
        }

        private void galerieItemSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void GalerieItemSpeichernButton_Click(object sender, RoutedEventArgs e)
        {
            GalerieItemSet myGalerieItem = new GalerieItemSet();

            myGalerieItem.Titel = GalerieItemTitelTextBox.Text;
            myGalerieItem.Kommentar = GalerieItemKommentarTextBox.Text;
            myGalerieItem.FotoSet = GalerieItemFotoTitelComboBox.SelectedItem as FotoSet;

            _context.FotoSets.Add(myGalerieItem);
            _context.GalerieItemSets.Add(myGalerieItem);
            _context.SubmitChanges(OnSubmitCompleted, null);

            GalerieItemSpeichernButton.IsEnabled = _context.HasChanges;
            GalerieItemVerwerfenButton.IsEnabled = _context.HasChanges;
        }

        private void GalerieItemVerwerfenButton_Click(object sender, RoutedEventArgs e)
        {
            _context.RejectChanges();

            GalerieItemTitelTextBox.Text = "";
            GalerieItemKommentarTextBox.Text = "";
            GalerieItemFotoTitelComboBox.SelectedIndex = -1;
        }

        private void galerieSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void galerieTypeSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void spielerSpeichernButton_Click(object sender, RoutedEventArgs e)
        {
            SpielerSet mySpieler = new SpielerSet();

            mySpieler.Name = spielerNameTextBox.Text;
            mySpieler.Vorname = spielerVornameTextBox.Text;
            mySpieler.Jahrgang = Int32.Parse(spielerJahrgangTextBox.Text);
            mySpieler.TeamId = (spielerTeamNameComboBox.SelectedItem as TeamSet).Id;

            if (spielerFotoUrlTextBox.Text.Length > 0)
            {
                FotoSet myFoto = new FotoSet();
                myFoto.Name = spielerNameTextBox.Text + ", " + spielerVornameTextBox.Text;

                myFoto.URL = spielerFotoUrlTextBox.Text;

                _context.FotoSets.Add(myFoto);
                mySpieler.FotoSets.Add(myFoto);
            }
            
            _context.SpielerSets.Add(mySpieler);
            _context.SubmitChanges(OnSubmitCompleted, null);

            spielerSpeichernButton.IsEnabled = _context.HasChanges;
            spielerVerwerfenButton.IsEnabled = _context.HasChanges;
        }

        private void spielerVerwerfenButton_Click(object sender, RoutedEventArgs e)
        {
            _context.RejectChanges();

            spielerNameTextBox.Text = "";
            spielerVornameTextBox.Text = "";
            spielerJahrgangTextBox.Text = "";
            spielerFotoUrlTextBox.Text = "";
            spielerTeamNameComboBox.SelectedIndex = -1;
        }

        private void teamTypeSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void ligaSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void teamSpeichernButton_Click(object sender, RoutedEventArgs e)
        {
            TeamSet myTeam = new TeamSet();

            myTeam.Name = teamNameTextBox.Text;
            myTeam.LigaSet = ligaNameComboBox.SelectedItem as LigaSet;
            myTeam.TeamTypeSet = teamTypeComboBox.SelectedItem as TeamTypeSet;
            myTeam.Ergebnisse = ergebnisseTextBox.Text;
            myTeam.Spielplan = spielplanTextBox.Text;
            myTeam.Statistik = statistikTextBox.Text;
            myTeam.Tabelle = tabelleTextBox.Text;

            _context.FotoSets.Add(myTeam);
            _context.TeamSets.Add(myTeam);
            _context.SubmitChanges(OnSubmitCompleted, null);

            teamSpeichernButton.IsEnabled = _context.HasChanges;
            teamVerwerfenButton.IsEnabled = _context.HasChanges;
        }

        private void teamVerwerfenButton_Click(object sender, RoutedEventArgs e)
        {
            _context.RejectChanges();

            teamNameTextBox.Text = "";
            ligaNameComboBox.SelectedIndex = -1;
            teamTypeComboBox.SelectedIndex = -1;
            ergebnisseTextBox.Text = "";
            spielplanTextBox.Text = "";
            statistikTextBox.Text = "";
            tabelleTextBox.Text = "";
        }

    }
}

